import datetime
import multiprocessing
from util import *
import copy
from CloudSystem import run_once
import gc


def generate_default_args():
    args=Args()
    args.request_num=10000
    args.pre_request_num=10000
    args.out_dir="output_main"
    
    return args


def run_once_this(method, trace_id,spec_reliability):
    print(f"start once {method}, trace_id:{trace_id}")
    args=generate_default_args()
    args.deploy_schedule_strategy=method
    args.trace_id=trace_id        
    args_copy=copy.deepcopy(args)
    args.spec_reliability=spec_reliability
    run_once(args_copy)
    gc.collect()
    print(f"End once {method}, trace_id:{trace_id}")
    

task_args_list=[]


for method in [ "ours", "RR","GSMS","R_RIR", "C_GM", "QFEC"]:# "RR","GSMS","R_RIR", "C_GM", "QFEC"
    for trace_id in range(10):
        for spec_reliability in [0.99, 0.9999]:

            task_args_list.append((method, trace_id, spec_reliability))
        
                

with multiprocessing.Pool(processes=32) as pool:
        interval=0
        results=[]
        index=0
        while index<len(task_args_list):
            # length=min(batch_size, len(task_args_list)-index )
            result=pool.apply_async(run_once_this, task_args_list[index])
            results.append(result)
            if index!=0 and index%5==0:
                time.sleep(interval)
            index+=1
            
        for result in results:
            result.get()


    
print("End all sub threadings")
